package com.xxxy.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.xxxy.blockhandler.monitorBlockHandler;
import com.xxxy.circuitBreaker.circuitBreakerHandler;
import com.xxxy.hotspotBlockHandler.hotspotBlockHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class testController {
    /**
     * Sentinel资源注解，用于定义资源的限流、降级等监控配置
     *
     * value 资源名称，标识该资源在Sentinel中的唯一标识符
     * blockHandler 限流处理方法名，当触发限流时调用的方法名称
     * blockHandlerClass 限流处理方法所在的类，指定处理方法的实现类
     *
     * 该注解将方法标记为Sentinel监控的资源，当访问该资源时会根据配置的规则进行流量控制，
     * 如果触发限流则会调用指定的处理方法进行降级处理。
     */
    @GetMapping("/test")
    @SentinelResource(value = "helloHandleTest",blockHandler = "helloHandle",blockHandlerClass = monitorBlockHandler.class)
    public String test(){
        return "hello world";
    }


    //    关联接口
    @GetMapping("/test2")
    public String test2(){
        return "关联接口！";
    }

    //    熔断测试
    @GetMapping("/test3")
    @SentinelResource(value = "test3",fallback = "test3Fallback",fallbackClass = circuitBreakerHandler.class)
    public String test3(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return "熔断测试！";
    }

    //    热点测试
    @GetMapping("/test4")
    @SentinelResource(value = "test4",blockHandler = "hotspotHandle",blockHandlerClass = hotspotBlockHandler.class)
    public String test4(@RequestParam(value = "name",required = false) String name,
                        @RequestParam(value = "type",required = false) String type){
        return "热点测试！";
    }
}